<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel=StyleSheet HREF="../stylesheet/stylesheet.css" TYPE="text/css" MEDIA=screen>
<title>Shop Management System</title>
<script>
function SetFocus(){
	document.getElementById('quantityadd').focus(); 
}
</script>
<?php include('menu.php'); ?>
</head>
<body onload="SetFocus();">
<?php
include("../connect/connect.php");

$Query = "SELECT * FROM product,category WHERE product.category_id = category.category_id and product_id = '".$_GET["id"]."'";
$objQuery = mysql_query($Query) or die ("Error Query [".$Query."]");
$row = mysql_fetch_array($objQuery);

//แก้ไข
if($_GET["action"]=='update'){
	if($_POST['quantityadd'] != "")
		{
			$Select = "SELECT quantity FROM product WHERE product_id = '".$_POST["product_id"]."' ";
			$Query = mysql_query($Select);
			$num = mysql_fetch_assoc($Query);
			
			if($num["quantity"] > $_POST['quantityadd']) {
			$quantity2 = $_POST["quantityadd"];
			$quantity1 = $_POST["quantity"];
			$quantity = $quantity1 - $quantity2;
			
			$Query = "UPDATE product SET quantity = '$quantity' WHERE product_id = '".$_POST["product_id"]."' ";
			$objQuery = mysql_query($Query);
			
			echo "<script type=\"text/javascript\">window.history.go(-2);</script>";
			exit();
			} else {
			$save = "มีจำนวนสินค้าไม่เพียงพอ กรุณากรอกสินค้าที่ต้องการเบิกใหม่";
			}
		} else {
		$save = "กรุณาระบุจำนวนสินค้าที่ต้องการเบิก";
		}
}
?>
<form action="?action=update&amp;id=<?php echo $_GET["id"]?>" name="formedit" method="post" id="formedit">
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading">
      <h1>เบิกสินค้า</h1>
      <div class="buttons"><a onclick="formedit.submit();" class="button"><span>บันทึก</span></a><a onclick="window.history.go(-1);" class="button"><span>ยกเลิก</span></a></div></div>
  <div class="content">
    <table class="form">
      <tr>
        <td>รหัสสินค้า : </td>
        <td><?php echo $row["code"];?>
		<input name="product_id" type="hidden" id="product_id" value="<?php echo $row["product_id"];?>" />
		<input name="quantity" type="hidden" id="quantity" value="<?php echo $row["quantity"];?>" /></td>
      </tr>
      <tr>
        <td>ชื่อสินค้า : </td>
        <td><?php echo $row["name"];?></td>
      </tr>
      <tr>
        <td>หมวดสินค้า : </td>
        <td><?php echo $row["category_name"];?></td>
      </tr>
      <tr>
        <td>จำนวน : </td>
        <td><input name="quantityadd" type="text" id="quantityadd" onKeyUp="IsNumeric(this.value,this)" /></td>
      </tr>
    </table>
	<?php echo "<br><font color=\"red\"><center>$save</center></font>"; ?>
  </div>
</div>
</form>
</body>
</html>
